#include "board.h"
#include<QPainter>

board::board(QWidget *parent) : QWidget(parent)
{
      for(int i=0;i<32;i++){
          a[i].init(i);
      }
}
//初始化棋子
void board::paintEvent(QPaintEvent *){
    QPainter painter(this);
    int d=40;//d为棋子直径
    r=d/2;
    //填充棋盘背景颜色
    painter.setBrush(QBrush(QColor(255,228,196)));
    painter.drawRect(d,d,8*d,9*d);
    //一共画十条横线
    for(int i=1;i<=10;i++){
        painter.drawLine(QPoint(d,i*d),QPoint(9*d,i*d));
    }
    //一共画九条竖线，但是中间有分界
    for(int i=1;i<=9;i++){
        if(i==9||i==1){
        painter.drawLine(QPoint(i*d,d),QPoint(i*d,10*d));
        }
        else{
            painter.drawLine(QPoint(i*d,d),QPoint(i*d,5*d));
            painter.drawLine(QPoint(i*d,6*d),QPoint(i*d,10*d));
        }
    }
    //画“将”“帅”所在格子
    painter.drawLine(QPoint(4*d,d),QPoint(6*d,3*d));
     painter.drawLine(QPoint(6*d,d),QPoint(4*d,3*d));
    painter.drawLine(QPoint(4*d,8*d),QPoint(6*d,10*d));
     painter.drawLine(QPoint(6*d,8*d),QPoint(4*d,10*d));
     //画32个棋子
     for(int i=0;i<32;i++){
         drawqizi(painter,i);
     }

}
QPoint board::locate(int row,int column){
    QPoint ret;
    ret.rx()=(column+1)*r*2;
    ret.ry()=(row+1)*r*2;
    return ret;
}
//获取坐标
QPoint board::locate(int loc){
    return locate(a[loc].row,a[loc].column);
}
//返回坐标值
void board::drawqizi(QPainter & painter,int loc){
    QPoint c=locate(loc);
    QRect rect=QRect(c.x()-r,c.y()-r,r*2,r*2);

    painter.setBrush(QBrush(QColor(222,184,135)));
    // 绘制棋子颜色

     painter.setPen(Qt::black);

    painter.drawEllipse(locate(loc),r,r);

    if(a[loc].color){
        painter.setPen(Qt::red);
    }
    painter.setFont(QFont("a",r,20));
    painter.drawText(rect,a[loc].getText(),QTextOption(Qt::AlignCenter));
}//绘制棋子
